<script setup>
import {User, Lock} from '@element-plus/icons-vue'
import {reactive, ref} from 'vue'
import router from "@/router";
import {loginTest} from "@/net";
import axios from "axios";
// 表单对象引用
const loginFormRef = ref()
// 定义表单数据对象，用于提交表单
const loginForm = reactive({
  username: 'admin',
  password: '123456'
})
// 定义表单校验规则
const loginFormRules = {
  // 验证用户名是否合法
  username: [
    {required: true, message: '请输入用户名', trigger: 'blur'},
    {min: 3, max: 20, message: '应为 3 ~ 20 个字符', trigger: 'blur'}
  ],
  // 验证密码是否合法
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
    {min: 6, max: 20, message: '应为 6 ~ 20 个字符', trigger: 'blur'}
  ]
}

function resetLoginForm() {
  // 重置表单
  loginFormRef.value.resetFields()
}

// 发送请求需要 验证表单提交数据是否合法
function userLogin() {
  // 校验表单 isValid
  loginFormRef.value.validate(async isValid => {
    if (isValid) {
      const {data: result} = await axios.post('/login', loginForm)
      if (result.meta.status === 200) {
        ElMessage.success('登录成功,欢迎您的到来！')
        sessionStorage.setItem('token', result.data.token)
        router.push('/home')
      } else {
        ElMessage.error('登录失败,请检查您的用户名和密码')
      }
    }
  })
}

</script>

<template>
  <div class="login_container">
    <div class="login_box">
      <!--      头像区域-->
      <div class="avatar_box">
        <img src="../assets/logo.svg" alt="vue">
      </div>
      <!--      登录表单区域-->
      <el-form ref="loginFormRef" :rules="loginFormRules" :model="loginForm" class="login_form" label-width="0px">
        <el-form-item prop="username">
          <el-input type="text" placeholder="用户名" v-model="loginForm.username">
            <template #prefix>
              <el-icon>
                <User/>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" placeholder="密码" v-model="loginForm.password">
            <template #prefix>
              <el-icon>
                <Lock/>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="userLogin()">登录</el-button>
          <el-button type="info" @click="resetLoginForm()">重置</el-button>

        </el-form-item>
      </el-form>


    </div>
  </div>
</template>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }

  }
}

.btns {
  display: flex;
  justify-content: flex-end;

}

.login_form {
  position: absolute;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
  bottom: 0;
}


</style>
